-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
135 lines (113 loc) · 3.19 KB
/
code.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Global array to hold all bubble objects
let bubbles;
// Store mouse press position so that a bubble can be created there
let mousePressX = 0;
let mousePressY = 0;
// Remember whether bubble is currently being created
let creatingBubble = false;
// Convert saved Bubble data into Bubble Objects
function loadData(bubblesData) {
bubbles = [];
for (let bubble of bubblesData) {
// Get x,y from position
let x = bubble.x;
let y = bubble.y;
// Get radius and name
let radius = bubble.radius;
let name = bubble.name;
// Put object in array
bubbles.push(new Bubble(x, y, radius, name));
}
}
function setup() {
let p5Canvas = createCanvas(640, 360);
// Get saved data
let savedData = getItem('bubbles');
// If no data has been saved yet
if (savedData === null) {
// Use an empty array to start
loadData([]);
} else {
// Otherwise convert the data to Bubble objects
loadData(savedData);
}
// When canvas is clicked, call saveMousePress()
p5Canvas.mousePressed(saveMousePress);
ellipseMode(RADIUS);
textSize(20);
describe(
'When the cursor clicks on the canvas, drags, and releases, a black outline circle representing a bubble appears on the white background. A prompt asks to name the bubble, and this name appears under the circle when the cursor hovers over it.'
);
}
function draw() {
background(255);
// Display all bubbles
for (let bubble of bubbles) {
bubble.display();
}
// Display bubble in progress
if (creatingBubble === true) {
let radius = dist(mousePressX, mousePressY, mouseX, mouseY);
noFill();
stroke(0);
strokeWeight(4);
circle(mousePressX, mousePressY, radius);
}
// Label directions at bottom
textAlign(LEFT, BOTTOM);
fill(0);
noStroke();
text('Click and drag to add bubbles.', 10, height - 10);
}
// Save current mouse position to use as next bubble position
function saveMousePress() {
mousePressX = mouseX;
mousePressY = mouseY;
creatingBubble = true;
}
// Add a bubble if in the process of creating one
function mouseReleased() {
if (creatingBubble === true) {
addBubble();
creatingBubble = false;
}
}
// Create a new bubble each time the mouse is clicked.
function addBubble() {
// Add radius and label to bubble
let radius = dist(mousePressX, mousePressY, mouseX, mouseY);
let name = prompt('Enter a name for the new bubble');
// If the user pressed 'Okay' and not 'Cancel'
if (name !== null) {
// Append the new JSON bubble object to the array
bubbles.push(new Bubble(mousePressX, mousePressY, radius, name));
storeItem('bubbles', bubbles);
}
}
// Bubble class
class Bubble {
constructor(x, y, radius, name) {
this.x = x;
this.y = y;
this.radius = radius;
this.name = name;
}
// Check if mouse is over the bubble
mouseOver() {
let mouseDistance = dist(mouseX, mouseY, this.x, this.y);
return mouseDistance < this.radius;
}
// Display the bubble
display() {
stroke(0);
noFill();
strokeWeight(4);
circle(this.x, this.y, this.radius);
if (this.mouseOver() === true) {
fill(0);
noStroke();
textAlign(CENTER);
text(this.name, this.x, this.y + this.radius + 30);
}
}
}